<?php
define("EW_PAGE_ID", "report", TRUE); // Page ID
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>

<?php 
//$ID = @$_GET["ID"]; 
//echo "ID = ".$ID;
?>

<?php

// PHPMaker 5 configuration for Table ActivitySummary
$ActivitySummary = new cActivitySummary; // Initialize table object

// Define table class
class cActivitySummary {

	// Define table level constants
	var $TableVar;
	var $TableName;
	var $SelectLimit = FALSE;
	var $Lookup;
	var $Name;
	var $Amount;
	var $ParentId;
	var $fields = array();

	function cActivitySummary() {
		$this->TableVar = "ActivitySummary";
		$this->TableName = "ActivitySummary";
		$this->Lookup = new cField('ActivitySummary', 'x_Lookup', 'Lookup', "`Lookup`", 200, -1, FALSE);
		$this->fields['Lookup'] =& $this->Lookup;
		$this->Name = new cField('ActivitySummary', 'x_Name', 'Name', "`Name`", 200, -1, FALSE);
		$this->fields['Name'] =& $this->Name;
		$this->Amount = new cField('ActivitySummary', 'x_Amount', 'Amount', "`Amount`", 5, -1, FALSE);
		$this->fields['Amount'] =& $this->Amount;
		$this->ParentId = new cField('ActivitySummary', 'x_ParentId', 'ParentId', "`ParentId`", 3, -1, FALSE);
		$this->fields['ParentId'] =& $this->ParentId;
	}




	// Report Group Level SQL
	function SqlGroupSelect() { // Select
		return "SELECT DISTINCT `Lookup`,`Name` FROM `view_summary_activity` ";
	}

	function SqlGroupWhere() { // Where
		return "ParentId =".@$_GET["ID"];
	}

	function SqlGroupGroupBy() { // Group By
		return "";
	}

	function SqlGroupHaving() { // Having
		return "";
	}

	function SqlGroupOrderBy() { // Order By
		return "`Lookup` ASC,`Name` ASC";
	}

	// Report Detail Level SQL
	function SqlDetailSelect() { // Select
		return "SELECT * FROM `view_summary_activity` ";
	}

	function SqlDetailWhere() { // Where
		return "ParentId =".@$_GET["ID"];
	}

	function SqlDetailGroupBy() { // Group By
		return "";
	}

	function SqlDetailHaving() { // Having
		return "";
	}

	function SqlDetailOrderBy() { // Order By
		return "";
	}

	// SQL variables
	var $CurrentFilter; // Current filter
	var $CurrentOrder; // Current order
	var $CurrentOrderType; // Current order type

	// Return report group sql
	function GroupSQL() {
		$sFilter = $this->CurrentFilter;
		$sSort = "";
		return ew_BuildSql($this->SqlGroupSelect(), $this->SqlGroupWhere(),
			 $this->SqlGroupGroupBy(), $this->SqlGroupHaving(),
			 $this->SqlGroupOrderBy(), $sFilter, $sSort);
	}

	// Return report detail sql
	function DetailSQL() {
		$sFilter = $this->CurrentFilter;
		$sSort = "";
		return ew_BuildSql($this->SqlDetailSelect(), $this->SqlDetailWhere(),
			$this->SqlDetailGroupBy(), $this->SqlDetailHaving(),
			$this->SqlDetailOrderBy(), $sFilter, $sSort);
	}

	// Return url
	function getReturnUrl() {
		if (@$_SESSION[EW_PROJECT_NAME . "_" . $this->TableVar . "_" . EW_TABLE_RETURN_URL] <> "") {
			return $_SESSION[EW_PROJECT_NAME . "_" . $this->TableVar . "_" . EW_TABLE_RETURN_URL];
		} else {
			return "ActivitySummarylist.php";
		}
	}

	function setReturnUrl($v) {
		$_SESSION[EW_PROJECT_NAME . "_" . $this->TableVar . "_" . EW_TABLE_RETURN_URL] = $v;
	}

	// View url
	function ViewUrl() {
		return $this->KeyUrl("ActivitySummaryview.php");
	}

	// Edit url
	function EditUrl() {
		return $this->KeyUrl("ActivitySummaryedit.php");
	}

	// Inline edit url
	function InlineEditUrl() {
		return $this->KeyUrl("ActivitySummarylist.php", "a=edit");
	}

	// Copy url
	function CopyUrl() {
		return $this->KeyUrl("ActivitySummaryadd.php");
	}

	// Inline copy url
	function InlineCopyUrl() {
		return $this->KeyUrl("ActivitySummarylist.php", "a=copy");
	}

	// Delete url
	function DeleteUrl() {
		return $this->KeyUrl("ActivitySummarydelete.php");
	}

	// Key url
	function KeyUrl($url, $action = "") {
		$sUrl = $url . "?";
		if ($action <> "") $sUrl .= $action . "&";
		return $sUrl;
	}

	// Function LoadRs
	// - Load Row based on Key Value
	function LoadRs($sFilter) {
		global $conn;

		// Set up filter (Sql Where Clause) and get Return Sql
		$this->CurrentFilter = $sFilter;
		$sSql = $this->SQL();
		return $conn->Execute($sSql);
	}

	// Load row values from rs
	function LoadListRowValues(&$rs) {
		$this->Lookup->setDbValue($rs->fields('Lookup'));
		$this->Name->setDbValue($rs->fields('Name'));
		$this->Amount->setDbValue($rs->fields('Amount'));
		$this->ParentId->setDbValue($rs->fields('ParentId'));
	}

	// Render list row values
	function RenderListRow() {
		global $conn;

		// Lookup
		$ActivitySummary->Lookup->ViewValue = $ActivitySummary->Lookup->CurrentValue;
		$ActivitySummary->Lookup->CssStyle = "";
		$ActivitySummary->Lookup->CssClass = "";
		$ActivitySummary->Lookup->ViewCustomAttributes = "";

		// Name
		$ActivitySummary->Name->ViewValue = $ActivitySummary->Name->CurrentValue;
		$ActivitySummary->Name->CssStyle = "";
		$ActivitySummary->Name->CssClass = "";
		$ActivitySummary->Name->ViewCustomAttributes = "";

		// Amount
		$ActivitySummary->Amount->ViewValue = $ActivitySummary->Amount->CurrentValue;
		$ActivitySummary->Amount->CssStyle = "";
		$ActivitySummary->Amount->CssClass = "";
		$ActivitySummary->Amount->ViewCustomAttributes = "";

		// Lookup
		$ActivitySummary->Lookup->HrefValue = "";

		// Name
		$ActivitySummary->Name->HrefValue = "";

		// Amount
		$ActivitySummary->Amount->HrefValue = "";
	}
	var $RowType; // Row Type
	var $CssClass; // Css class
	var $CssStyle; // Css style
	var $RowClientEvents; // Row client events

	// Display Attribute
	function DisplayAttributes() {
		$sAtt = "";
		if (trim($this->CssStyle) <> "") {
			$sAtt .= " style=\"" . trim($this->CssStyle) . "\"";
		}
		if (trim($this->CssClass) <> "") {
			$sAtt .= " class=\"" . trim($this->CssClass) . "\"";
		}
		if ($this->Export == "") {
			if (trim($this->RowClientEvents) <> "") {
				$sAtt .= " " . $this->RowClientEvents;
			}
		}
		return $sAtt;
	}

	// Export
	var $Export;

//	 ----------------
//	  Field objects
//	 ----------------
	function fields($fldname) {
		return $this->fields[$fldname];
	}
}
?>
<?php include "userfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php @set_time_limit(999); // Set the maximum execution time (seconds) ?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$ActivitySummary->Export = @$_GET["export"]; // Get export parameter
$sExport = $ActivitySummary->Export; // Get export parameter, used in header
$sExportFile = $ActivitySummary->TableVar; // Get export file, used in header
?>
<?php
if ($ActivitySummary->Export == "excel") {
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename=' . $sExportFile .'.xls');
}
if ($ActivitySummary->Export == "csv") {
	header('Content-Type: application/csv');
	header('Content-Disposition: attachment; filename=' . $sExportFile .'.csv');
}
?>
<?php
$nRecCount = 0;
$nGrpRecs = 0;
$nDtlnRecCount = 0;
$nDtlRecs = 0;
$sFilter = "";
$sDbMasterFilter = "";
$sDbDetailFilter = "";
$sCmd = "";
$vGrps = ew_InitArray(3, NULL);
$nCntRecs = ew_InitArray(3, 0);
$bLvlBreak = ew_InitArray(3, FALSE);
$nTotals = ew_Init2DArray(3, 2, 0);
$nMaxs = ew_Init2DArray(3, 2, 0);
$nMins = ew_Init2DArray(3, 2, 0);
?>

<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>


<?php if ($ActivitySummary->Export == "") { ?>
<?php } ?>
<p><span class="phpmaker">Report: Activity Summary
<?php if ($ActivitySummary->Export == "") { ?>
&nbsp;&nbsp;<a href="ActivitySummaryreport.php?export=excel">Export to Excel</a>
<?php } ?>
</span></p>
<form method="post">
<table class="ewReportTable" cellspacing="-1">
<?php
$sFilter = "";
if ($sDbDetailFilter <> "") {
	if ($sFilter <> "") $sFilter .= " AND ";
	$sFilter .= "(" . $sDbDetailFilter . ")";
}

// Set up filter and load Group level sql
$ActivitySummary->CurrentFilter = $sFilter;
$sSql = $ActivitySummary->GroupSQL();

// echo $sSql;
// Load recordset

$rs = $conn->Execute($sSql);
//echo $sSql;

// Get First Row
if (!$rs->EOF) {
	$ActivitySummary->Lookup->setDbValue($rs->fields('Lookup'));
	$vGrps[0] = $ActivitySummary->Lookup->DbValue;
	$ActivitySummary->Name->setDbValue($rs->fields('Name'));
	$vGrps[1] = $ActivitySummary->Name->DbValue;
}
$nRecCount = 0;
$nCntRecs[0] = 0;
ChkLvlBreak();
while (!$rs->EOF) {

	// Render for view
	$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
	RenderRow();

	// Show group headers
	if ($bLvlBreak[1]) { // Reset counter and aggregation
?>
	<tr><td colspan=2 class="ewGroupField"><span class="phpmaker">Lookup</span></td>
	<td colspan=1 class="ewGroupName"><span class="phpmaker">
<div<?php echo $ActivitySummary->Lookup->ViewAttributes() ?>><?php echo $ActivitySummary->Lookup->ViewValue ?></div>
</span></td></tr>
<?php
	}
	if ($bLvlBreak[2]) { // Reset counter and aggregation
?>
	<tr><td style="width: 5px;"></td><td class="ewGroupField"><span class="phpmaker">Name</span></td>
	<td colspan=1 class="ewGroupName"><span class="phpmaker">
<div<?php echo $ActivitySummary->Name->ViewAttributes() ?>><?php echo $ActivitySummary->Name->ViewValue ?></div>
</span></td></tr>
<?php
	}

	// Get detail records
	$sFilter = "";
	if ($sFilter <> "") $sFilter .= " AND ";
	if (is_null($ActivitySummary->Lookup->CurrentValue)) {
		$sFilter .= "(`Lookup` IS NULL)";
	} else {
		$sFilter .= "(`Lookup` = '" . ew_AdjustSql($ActivitySummary->Lookup->CurrentValue) . "')";
	}
	if ($sFilter <> "") $sFilter .= " AND ";
	if (is_null($ActivitySummary->Name->CurrentValue)) {
		$sFilter .= "(`Name` IS NULL)";
	} else {
		$sFilter .= "(`Name` = '" . ew_AdjustSql($ActivitySummary->Name->CurrentValue) . "')";
	}
	if ($sDbDetailFilter <> "") {
		if ($sFilter <> "") $sFilter .= " AND ";
		$sFilter .= "(" . $sDbDetailFilter . ")";
	}

	// Set up detail SQL
	$ActivitySummary->CurrentFilter = $sFilter;
	$sSql = $ActivitySummary->DetailSQL()." and ParentId =".@$_GET["ID"];
//echo $sSql ;
	// Load detail records
	$rsdtl = $conn->Execute($sSql);
	$nDtlRecs = $rsdtl->RecordCount();

	// Initialize Aggregate
	if (!$rsdtl->EOF) {
		$nRecCount++;
		$ActivitySummary->Amount->setDbValue($rsdtl->fields('Amount'));
	}
	if ($nRecCount == 1) {
		$nCntRecs[0] = 0;
		$nTotals[0][0] = 0;
	}
	for ($i = 1; $i <= 2; $i++) {
		if ($bLvlBreak[$i]) { // Reset counter and aggregation
			$nCntRecs[$i] = 0;
			$nTotals[$i][0] = 0;
		}
	}
	$nCntRecs[0] += $nDtlRecs;
	$nCntRecs[1] += $nDtlRecs;
	$nCntRecs[2] += $nDtlRecs;
?>
	<tr>
		<td style="width: 5px;"></td>
		<td style="width: 5px;"></td>
		<td valign="top" class="ewGroupHeader"><span class="phpmaker">Amount</span></td>
	</tr>
<?php
	while (!$rsdtl->EOF) {
		$ActivitySummary->Amount->setDbValue($rsdtl->fields('Amount'));
		$nTotals[0][0] += $ActivitySummary->Amount->CurrentValue;
		$nTotals[1][0] += $ActivitySummary->Amount->CurrentValue;
		$nTotals[2][0] += $ActivitySummary->Amount->CurrentValue;

		// Render for view
		$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
		RenderRow();
?>
	<tr>
		<td style="width: 5px;"></td>
		<td style="width: 5px;"></td>
		<td><span class="phpmaker">
<div<?php echo $ActivitySummary->Amount->ViewAttributes() ?>><?php echo $ActivitySummary->Amount->ViewValue ?></div>
</span></td>
	</tr>
<?php
		$rsdtl->MoveNext();
	}
	$rsdtl->Close();

	// Save old group data
	$vGrps[0] = $ActivitySummary->Lookup->CurrentValue;
	$vGrps[1] = $ActivitySummary->Name->CurrentValue;

	// Get next record
	$rs->MoveNext();
	if ($rs->EOF) {
		$nRecCount = 0; // EOF, force all level breaks
	} else {
		$ActivitySummary->Lookup->setDbValue($rs->fields('Lookup'));
		$ActivitySummary->Name->setDbValue($rs->fields('Name'));
	}
	ChkLvlBreak();

	// Show Footers
	if ($bLvlBreak[2]) {
		$ActivitySummary->Name->CurrentValue = $vGrps[1];

		// Render row for view
		$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
		RenderRow();
		$ActivitySummary->Name->CurrentValue = $ActivitySummary->Name->DbValue;
?>
	<tr><td style="width: 5px;"></td><td colspan=2 class="ewGroupSummary"><span class="phpmaker">Summary for Name: <?php echo $ActivitySummary->Name->ViewValue ?> (<?php echo ew_FormatNumber($nCntRecs[2],0) ?> Detail Records)</span></td></tr>
<?php
	$ActivitySummary->Amount->CurrentValue = $nTotals[2][0];

	// Render row for view
	$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
	RenderRow();
?>
	<tr><td style="width: 5px;"></td>
		<td class="ewGroupAggregate"><span class="phpmaker">Sum</span></td>
		<td><span class="phpmaker">
<div<?php echo $ActivitySummary->Amount->ViewAttributes() ?>><?php echo $ActivitySummary->Amount->ViewValue ?></div>
</span></td>
	</tr>
<?php
}
	if ($bLvlBreak[1]) {
		$ActivitySummary->Lookup->CurrentValue = $vGrps[0];

		// Render row for view
		$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
		RenderRow();
		$ActivitySummary->Lookup->CurrentValue = $ActivitySummary->Lookup->DbValue;
?>
	<tr><td colspan=3 class="ewGroupSummary"><span class="phpmaker">Summary for Lookup: <?php echo $ActivitySummary->Lookup->ViewValue ?> (<?php echo ew_FormatNumber($nCntRecs[1],0) ?> Detail Records)</span></td></tr>
<?php
	$ActivitySummary->Amount->CurrentValue = $nTotals[1][0];

	// Render row for view
	$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
	RenderRow();
?>
	<tr>
		<td colspan=2 class="ewGroupAggregate"><span class="phpmaker">Sum</span></td>
		<td><span class="phpmaker">
<div<?php echo $ActivitySummary->Amount->ViewAttributes() ?>><?php echo $ActivitySummary->Amount->ViewValue ?></div>
</span></td>
	</tr>
<?php
}
}

// Close recordset
$rs->Close();
?>

	<tr>
		<td colspan=2 class="ewGroupAggregate"><span class="phpmaker">IPPSaleAmt</span></td>
		<td><span class="phpmaker"><div><?php echo $_GET["IPPSaleAmt"]; ?></div></span></td>
	</tr>
	<tr>
		<td colspan=2 class="ewGroupAggregate"><span class="phpmaker">XReading</span></td>
		<td><span class="phpmaker"><div><?php echo $_GET["XReadingAmt"]; ?></div></span></td>
	</tr>
	<tr><td colspan=3 class="ewGrandSummary"><span class="phpmaker">Grand Total (<?php echo ew_FormatNumber($nCntRecs[0],0) ?> Detail Records)</span></td></tr>
<?php
	$ActivitySummary->Amount->CurrentValue = $nTotals[0][0];

	// Render row for view
	$ActivitySummary->RowType = EW_ROWTYPE_VIEW;
	RenderRow();
?>
	<tr>
		<td colspan=2 class="ewGroupAggregate"><span class="phpmaker">Sum</span></td>
		<td><span class="phpmaker">
<div<?php echo $ActivitySummary->Amount->ViewAttributes() ?>><?php echo $ActivitySummary->Amount->ViewValue+ $_GET["IPPSaleAmt"]+$_GET["XReadingAmt"] ?></div>
</span></td>
	</tr>
	<tr><td colspan=3><span class="phpmaker">&nbsp;<br></span></td></tr>
</table>
</form>
<?php

// Check level break
function ChkLvlBreak() {
	global $nRecCount, $bLvlBreak, $vGrps, $ActivitySummary;
	$bLvlBreak[1] = FALSE;
	$bLvlBreak[2] = FALSE;
	if ($nRecCount == 0) { // Start Or End of Recordset
		$bLvlBreak[1] = TRUE;
		$bLvlBreak[2] = TRUE;
	} else {
		if (!ew_CompareValue($ActivitySummary->Lookup->CurrentValue, $vGrps[0])) {
			$bLvlBreak[1] = TRUE;
			$bLvlBreak[2] = TRUE;
		}
		if (!ew_CompareValue($ActivitySummary->Name->CurrentValue, $vGrps[1])) {
			$bLvlBreak[2] = TRUE;
		}
	}
}
?>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>

<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $ActivitySummary;

	// Common render codes for all row types
	// Lookup

	$ActivitySummary->Lookup->CellCssStyle = "";
	$ActivitySummary->Lookup->CellCssClass = "";

	// Name
	$ActivitySummary->Name->CellCssStyle = "";
	$ActivitySummary->Name->CellCssClass = "";

	// Amount
	$ActivitySummary->Amount->CellCssStyle = "";
	$ActivitySummary->Amount->CellCssClass = "";
	if ($ActivitySummary->RowType == EW_ROWTYPE_VIEW) { // View row

		// Lookup
		$ActivitySummary->Lookup->ViewValue = $ActivitySummary->Lookup->CurrentValue;
		$ActivitySummary->Lookup->CssStyle = "";
		$ActivitySummary->Lookup->CssClass = "";
		$ActivitySummary->Lookup->ViewCustomAttributes = "";

		// Name
		$ActivitySummary->Name->ViewValue = $ActivitySummary->Name->CurrentValue;
		$ActivitySummary->Name->CssStyle = "";
		$ActivitySummary->Name->CssClass = "";
		$ActivitySummary->Name->ViewCustomAttributes = "";

		// Amount
		$ActivitySummary->Amount->ViewValue = $ActivitySummary->Amount->CurrentValue;
		$ActivitySummary->Amount->CssStyle = "";
		$ActivitySummary->Amount->CssClass = "";
		$ActivitySummary->Amount->ViewCustomAttributes = "";

		// Lookup
		$ActivitySummary->Lookup->HrefValue = "";

		// Name
		$ActivitySummary->Name->HrefValue = "";

		// Amount
		$ActivitySummary->Amount->HrefValue = "";
	} elseif ($ActivitySummary->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($ActivitySummary->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($ActivitySummary->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
